package collections;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

public class VectorVsListVsLinkedList {

	
	long times = 1000000;//100W
	long removeTimes = 10000;
	String item = "a";
	
	Vector<String> vector = new Vector<String>();
	List<String> list = new ArrayList<String>();
	LinkedList<String> linkedList = new LinkedList<String>();
	
	public static void main(String[] args) {
		
		VectorVsListVsLinkedList obj = new VectorVsListVsLinkedList();
		
		System.out.println("add item at end position: vector vs list vs linkedList ");
		obj.vectorAdd(-1);
		obj.listAdd(-1);
		obj.linkedListAdd(-1);
		
		System.out.println("get item: vector vs list vs linkedList");
		int position = 100;
		obj.vectorGet(position);
		obj.listGet(position);
		obj.linkedListGet(position);
		
		
		System.out.println("remove item: vector vs list vs linkedList");
		position = 101;
		obj.vectorRemove(position);
		obj.listRemove(position);
		obj.linkedListRemove(position);
	}
	
	
	
	 void vectorAdd(int position){
		
		long start = System.currentTimeMillis();
		
	    for(long i = 0; i < times; i++){
	    	
	    	if(position == -1){
	    		vector.add(item);
	    	}else{
	    		vector.add(position, item);
	    	}
	    }
		
		System.out.println("vector add:"+(System.currentTimeMillis()-start)+"ms");
		
	}
	
	 void listAdd(int position){
		
		long start = System.currentTimeMillis();
		
		 for(long i = 0; i < times; i++){
		    	
		       if(position == -1){
		    	   list.add(item);
		       }else{
		    	   list.add(position, item);
		       }
		    }
		System.out.println("arrayList add:"+(System.currentTimeMillis()-start)+"ms");
		
	}
	
	
	 void linkedListAdd(int position){
		
		long start = System.currentTimeMillis();
		 for(long i = 0; i < times; i++){
		    	
			 if(position == -1){
				 linkedList.add(item);
			 }else{
				 linkedList.add(position,item);
			 }
			 
		 }
		
		System.out.println("linked list add:"+(System.currentTimeMillis()-start)+"ms");
		
	}
	 
	 void vectorGet(int position){
			
			long start = System.currentTimeMillis();
			
		    for(long i = 0; i < times; i++){
		    	
		    	vector.get(position);
		    }
			
			System.out.println("vector get:"+(System.currentTimeMillis()-start)+"ms");
			
		}
		
		 void listGet(int position){
			
			long start = System.currentTimeMillis();
			
			 for(long i = 0; i < times; i++){
			    	
			        list.get(position);
			    }
			System.out.println("arrayList add:"+(System.currentTimeMillis()-start)+"ms");
			
		}
		
		
		 void linkedListGet(int position){
			
			long start = System.currentTimeMillis();
			 for(long i = 0; i < times; i++){
			    	
				linkedList.get(position);
				 
			 }
			
			System.out.println("linked list add:"+(System.currentTimeMillis()-start)+"ms");
			
		}
		  
		 void vectorRemove(int position){
				
				long start = System.currentTimeMillis();
				
			    for(long i = 0; i < removeTimes; i++){
			    	
			    	vector.remove(position);
			    }
				
				System.out.println("vector get:"+(System.currentTimeMillis()-start)+"ms");
				
			}
			
			 void listRemove(int position){
				
				long start = System.currentTimeMillis();
				
				 for(long i = 0; i < removeTimes; i++){
				    	
				        list.remove(position);
				    }
				System.out.println("arrayList add:"+(System.currentTimeMillis()-start)+"ms");
				
			}
			
			
			 void linkedListRemove(int position){
				
				long start = System.currentTimeMillis();
				 for(long i = 0; i < removeTimes; i++){
				    	
					linkedList.remove(position);
					 
				 }
				
				System.out.println("linked list add:"+(System.currentTimeMillis()-start)+"ms");
				
			}
}
